package com.google.jessewilson.codesearch.configuration;

import com.google.inject.Inject;
import com.google.jessewilson.codesearch.api.SearchProvider;
import com.google.jessewilson.codesearch.api.SearchProviderFactory;

import java.util.ListIterator;

/**
 * @author jessewilson
 */
public class ConfigurationManager {

  private Configuration current;

  @Inject
  public ConfigurationManager(SearchProviderFactory searchProviderFactory) {
    this.current = createDefault(searchProviderFactory);
  }

  public synchronized void setCurrent(Configuration activeInstance) {
    this.current = activeInstance;
  }

  public synchronized Configuration getCurrent() {
    return current;
  }

  private Configuration createDefault(SearchProviderFactory searchProviderFactory) {
    Configuration.Builder builder = new Configuration.Builder();

    for (ListIterator<SearchProvider> i
        = searchProviderFactory.getAllSearchProviders().listIterator(); i.hasNext(); ) {
      boolean enabled = !i.hasPrevious();
      builder.addConfiguration(i.next().createDefaultConfiguration(), enabled);
    }

    return builder.build();
  }
}
